/**
 * 用户管理
 */
var Permission = function() {

	var me = this;

	return {

		init : function() {
		},

		search : function() {
			$('#data-grid').treegrid({
				url : '/admin/permission/search',
				queryParams : App.dataGridQueryParams('search-form')
			});
		},

		/** 添加权限模块 */
		beforeAdd : function() {
			$('#add-dialog').dialog('open');
			$('#add-form').form('clear');
			App.selectRadio('type', '0');
		},

		beforeAddChild : function() {
			var row = $('#data-grid').treegrid('getSelected');
			if (!row) {
				$.messager.alert('Warning', '请选择要操作的对象');
				return;
			}
			if (row.type == '1') {
				$.messager.alert('Warning', '按钮没有下级菜单');
				return;
			}
			$('#add-form').form('clear');
			$('#parentId').val(row.id);
			$('#add-dialog').dialog('open');
			if (row.parentId == '0') {
				App.selectRadio('type', '0');
			} else {
				App.selectRadio('type', '1');
			}
		},

		addPermission : function() {
			$('#add-form').form('submit', {
				url : '/admin/permission/save',
				onSubmit : function() {
					return $(this).form('validate');
				},
				success : function(result) {
					result = $.parseJSON(result);
					if (result.success == true) {
						$('#add-dialog').dialog('close');
						$('#data-grid').treegrid('reload');
					}
					$.messager.show({
						title : '信息',
						msg : result.data
					});
				}
			});
		},

		/** 修改准备 */
		beforeUpdate : function() {
			var row = $('#data-grid').treegrid('getSelected');
			if (row) {
				$('#add-dialog').dialog('open').dialog('setTitle', '编辑');
				$('#add-form').form('load',
						'/admin/permission/queryById?id=' + row.id);
				$('#permission').textbox('readonly');
			} else {
				$.messager.alert('Warning', '请选择要修改的数据');
			}
		},

		/** 删除 */
		deletePermission : function() {
			var row = $('#data-grid').treegrid('getSelected');
			if (row) {
				$.messager.confirm('Confirm', '确定要删除此信息吗？', function(ok) {
					if (ok) {
						$.post('/admin/permission/delete', {
							id : row.id
						}, function(result) {
							$.messager.show({
								title : '信息',
								msg : result.data
							});
							$('#data-grid').treegrid('reload');
						}, 'json');
					}
				});
			} else {
				$.messager.alert('Warning', '请选择要删除的信息');
			}
		},

		/** 右键菜单 */
		rightMenu : function(e, row) {
			e.preventDefault();
			$(this).treegrid('select', row.id);
			$('#rightMenu').menu('show', {
				left : e.pageX,
				top : e.pageY
			});
		},

		formatType : function(value, row, index) {
			return value == "0" ? "菜单" : "按钮";
		},
		
		collapse : function() {
			$('#data-grid').treegrid('collapseAll');
		},
		
		expand : function() {
			$('#data-grid').treegrid('expandAll');
		}
	}

}();